<template>
  <el-row type="flex" justify="center" class="bg">
    <div class="reg">
      <h2 style="text-align:center;margin:0">注册</h2>
      <el-form
        ref="formData"
        :model="formData"
        :rules="rules"
        label-width="80px"
        @keyup.enter.native="register()"
      >
        <el-form-item prop="email" label="邮箱">
          <el-input
            v-model="formData.email"
            placeholder="请输入邮箱"
            prefix-icon="el-icon-userr"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="code" label="验证码">
          <div class="very-code">
            <el-input
              type="text"
              v-model="formData.code"
              prefix-icon="icon-login_pwd"
              clearable
              placeholder="请输入验证码"
            />
            <el-button type="success" @click="getCode">获取验证码</el-button>
          </div>
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input
            v-model="formData.password"
            placeholder="请输入密码"
            type="password"
            prefix-icon="icon-login-pwd"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="cheackPassword" label="确认密码">
          <el-input
            v-model="formData.cheackPassword"
            placeholder="再次输入密码"
            type="password"
            prefix-icon="icon-login_pwd"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="register('formData')" icon="el-icon-upload">注册</el-button>
          <el-button @click="resetForm('formData')">重置</el-button>
        </el-form-item>
        <router-link style="position:relative;left:100px;" to="login">已有密码？登录</router-link>
      </el-form>
    </div>
  </el-row>
</template>
<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.formData.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      formData: {},
      rules: {
        email: [
          { required: true, message: "邮箱不能为空不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        cheackPassword: [
          { required: true, validator: validatePass, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    async getCode() {
        if(this.formData.email){
            const res = await this.$http.post("/users/code", {
                email: this.formData.email
              });
            if(res.code === 0) {
              this.$message({
                message: "发送成功",
                type: "success"
              });
            }
        } else {
            this.$message({
                message: "邮箱不可为空",
                type: "error"
             });
        }
    },

    register(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          await this.$http.post("/users/register", { ...this.formData });

          this.$message({
            type: "success",
            message: "注册成功"
          });
          this.formData = {};
          this.$router.push("/register");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
<style lang="less" scoped>
@import "../assets/style/register";
</style>